package com.example.mypro;

import java.util.List;

import com.example.domain.JiaoShi;
import com.example.factory.ServiceFactory;
import com.example.service.InfoService;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class YiFuActivity extends Activity {
	private ListView lv;
	private ProgressBar pb;
	private InfoService infoService;
	private MyTask task;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		infoService = ServiceFactory.getInfoService();
		setContentView(R.layout.layout_yifu);
		lv = (ListView) findViewById(R.id.lv_yifu);
		pb = (ProgressBar) findViewById(R.id.pb_yifu);
		lv.setVisibility(View.INVISIBLE);
		pb.setVisibility(View.VISIBLE);
		task = new MyTask();
		task.execute();
	}

	class MyTask extends AsyncTask<Void, Void, List<JiaoShi>> {

		@Override
		protected List<JiaoShi> doInBackground(Void... params) {
			List<JiaoShi> list = infoService.getJiaoShiByLouYu("逸夫楼");
			return list;
		}

		@Override
		protected void onPostExecute(List<JiaoShi> list) {

			// System.out.println("onProgressUpdate");
			if (list != null) {
				lv.setVisibility(View.VISIBLE);
				pb.setVisibility(View.INVISIBLE);
				ListAdapter adapter = new ListAdapter(list);
				lv.setAdapter(adapter);
			}
		}
	}

	class ListAdapter extends BaseAdapter {
		private List<JiaoShi> list;

		public ListAdapter(List<JiaoShi> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@SuppressWarnings("unused")
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			ViewHolder holder = null;
			if (holder == null) {
				holder = new ViewHolder();
				arg1 = LayoutInflater.from(YiFuActivity.this).inflate(
						R.layout.layout_jiaoshi, null);
				holder.iv = (ImageView) arg1.findViewById(R.id.iv_kongxian);
				holder.tv1 = (TextView) arg1.findViewById(R.id.tv_jiaoshi);
				holder.tv2 = (TextView) arg1.findViewById(R.id.tv_shiyong);
			} else {
				holder = ((ViewHolder) arg1.getTag());
			}
			holder.tv1.setText(list.get(arg0).getJs_bh());
			if (list.get(arg0).getJs_sy().equals("1")) {
				holder.tv2.setText("上课中");
				holder.iv.setImageResource(R.drawable.poi_location_icon);
			} else {
				holder.tv2.setText("空闲");
				holder.iv.setImageResource(R.drawable.poi_mylocation_icon);
			}
			return arg1;
		}

	}

	class ViewHolder {
		private ImageView iv;
		private TextView tv1;
		private TextView tv2;
	}
}
